using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace LeetCodeSolutions
{
    /// <summary>
    /// 66. 加一
    /// </summary>
    public class LeetCode66: LeetCodeBase
    {
        public override void Execute()
        {
            int[] nums = { 1, 2, 3, };
            Display(nums);
        }

        public int[] PlusOne(int[] digits)
        {
            bool add = true; //需要+1
            for (int i = digits.Length - 1; i >= 0; i--)
            {
                if (add)
                {
                    digits[i]++;
                }
                
                if (digits[i] == 10)
                {
                    digits[i] = 0;
                    add = true;
                }
                else
                {
                    add = false;
                }
            }

            if (digits[0] == 0)
            {
                var list = digits.ToList();
                list.Insert(0, 1);
                digits = list.ToArray();
            }

            return digits;
        }

        public void Display(int[] digits)
        {
            digits = new[] { 9 };
            var results = PlusOne(digits);
            if (results == null)
            {
                return;
            }

            for (int i = 0; i < results.Length; i++)
            {
                CommonFuntions.Print(results[i]);
            }
        }
    }
}